<%--
  Created by IntelliJ IDEA.
  User: 我爱陈果果
  Date: 2020/8/16
  Time: 9:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="basePath.jsp"%>
<html>
<head>
    <title>首页</title>
</head>
<body>
    <a href="${basePath}/auth/logout">退出</a>
    <h1>欢迎：<span style="color: red;">${username}</span></h1>
    <shiro:hasRole name="SUPERADMINISTATOR">
        <div>超级管理员</div>
    </shiro:hasRole>

    <div shiro:hasRole="SUPERADMINISTATOR">
        <a href="${basePath}auth/updateAuthority">同步权限</a> |
        <a href="${basePath}auth/queryAllAuthority">查询权限</a>
        <!--分配角色-->
        <table border="1">
            <tr>
                <th>用户名</th>
                <th>是否锁定</th>
                <th>选择角色</th>
            </tr>
            <c:forEach items="${map}" var="userEntry">
                <tr>
                    <c:set var="user" value="${userEntry.key}"/>
                    <c:set var="userRoles" value="${userEntry.value}"/>
                    <td>${user.userName}</td>
                    <td>
                        <input type="radio" onclick="lock(${user.id},this)" ${user.userEnable==0?'checked':''} name="has_${user.id}" value="0"/>锁定
                        <input type="radio" onclick="lock(${user.id},this)" ${user.userEnable==1?'checked':''} name="has_+${user.id}" value="1"/>解锁
                    </td>
                    <td>
                        <c:forEach items="${roles}" var="role">
                            <c:set var="flag" value="false"/>
                            <c:forEach items="${userRoles}" var="userRole">
                                <%--只有相等的时候才显示--%>
                                <c:if test="${role.id==userRole.id}">
                                    <c:set var="flag" value="true"/>
                                    <input type="checkbox" onclick="giveRole(${user.id}, ${role.id}, this)" checked name="role_${user.id}" value="${role.roleDesc}"/>${role.roleDesc}
                                </c:if>
                            </c:forEach>
                            <%--而且角色已经显示了就不再显示--%>
                            <c:if test="${!flag}">
                                <input type="checkbox" onclick="giveRole(${user.id}, ${role.id}, this)" name="role_${user.id}" value="${role.roleDesc}"/>${role.roleDesc}
                            </c:if>
                        </c:forEach>
                    </td>
                </tr>
            </c:forEach>
        </table>
    </div>

    <shiro:hasRole name="ADMINISTATOR">
        <div>管理员</div>
    </shiro:hasRole>

    <shiro:hasRole name="USER">
        <div>普通用户</div>
    </shiro:hasRole>

</body>

<script>
    function lock(userId, obj) {
        window.location.href = "${basePath}auth/lock?userId="+userId+"&userEnable="+obj.value;
    }

    function giveRole(userId, roleId, obj) {
        window.location.href = "${basePath}auth/giveRole?userId="+userId+"&roleId="+roleId+"&checked="+obj.checked;
    }
</script>

</html>
